<?php
/**
 * news.php
 *
 * Tour类
 * @author konakona
 * @date 2009-9-30
 */
class news {
	/**
	 * 添加news
	 * @param string $_post
	*/
	public static function news_add($_post){
		if(strlen($_post['title'])<3){
			k::message('标题太短...','javascript:history.go(-1);');
		}
		if(strlen($_post['title'])>100){
			k::message('标题超过100字符，将不会显示，请适当修减','javascript:history.go(-1);');
		}
		if(strlen($_post['content'])<10){
			k::message('内容太少...','javascript:history.go(-1);');
		}
		$sql = "INSERT INTO news values('','{$_post[title]}','{$_post[content]}','".time()."',0)";
		mysql::connect()->query($sql);
		k::message('成功添加News!','javascript:history.go(-1);');
	}
	/**
	 * 删除news
	 * @param mixed $id
	*/
	public static function news_del($id){
		$sql = "DELETE FROM news WHERE nid=$id";
		mysql::connect()->query($sql);
		k::message('这条news已删除成功','javascript:history.go(-1);');
	}
	/**
	 * 列出所有news,默认一次10片
	 * @param int $pid $row
	 * @return array
	*/
	public static function news_list($pid,$row=10){
		if($pid == 0){
			$pid = 1;
		}
		$allcount = page::tableCount('news');
		if($pid==1){
			//如果是第一次访问
			$sql = "select * from news ORDER BY nid DESC limit ".($pid-1).",$row";
		}else{
			$page_start  = ($pid-1)+$row;
			$sql = "select * from news ORDER BY nid DESC limit ".($page_start-1).",$row";
		}
		//echo $sql;
		return mysql::connect()->fetch_all($sql);
	}
	/**
	 * 读取一条news信息
	 * @param int $name
	 * @return array
	*/
	public static function news_details($name){
		if(is_numeric($name)){
			$sql = "select * from news where nid=$name";
		}else{
			$sql = "select * from news where title='{$name}'";
		}
		$result = mysql::connect()->fetch_all($sql);
		if($result[0]==''){
			k::message("Sorry,Can 't found");
		}
		return $result[0];
	}
	/**
	 * 给一条news增加点击率
	 * @param int $id
	*/
	public static function news_clicknum($id){
		mysql::connect()->query("UPDATE `news` SET `clicknum` = `clicknum`+1 WHERE `nid` =$id");
	}
	/**
	 * 修改一条news信息
	 * @param int $_post
	*/
	public static function news_edit($_post){
		$title = trim($_post['title']);
		$sql = "UPDATE `news` SET `title` = '$title', `content` = '{$_post[content]}' where nid=$_post[id]";
		mysql::connect()->query($sql);
		k::message('修改成功','?action=news-list');
	}
}